﻿#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDebug>
#include <QLayout>
#include<QBarCategoryAxis>

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    //请一定要记得将柱状图指针初始化为NULL
    chartView=NULL;

    //布局页面
    InterfaceLayout();

    //绘制柱状图
    DrawBarChart(1, 2, 3, 4);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::InterfaceLayout()
{
    //创建一个水平布局存放左侧（各个输入框、按钮、label），以及柱状图
    HLayout = new QHBoxLayout;

    //创建一个垂直布局 单独存放输入框、按钮、label
    VLayout = new QVBoxLayout;
    VLayout->addWidget(ui->label);
    VLayout->addWidget(ui->lineEdit);
    VLayout->addWidget(ui->label_2);
    VLayout->addWidget(ui->lineEdit_2);
    VLayout->addWidget(ui->label_3);
    VLayout->addWidget(ui->lineEdit_3);
    VLayout->addWidget(ui->label_4);
    VLayout->addWidget(ui->lineEdit_4);
    VLayout->addWidget(ui->btn_submit);

    //将左侧的垂直布局以1/5的占比放入主窗口中
    HLayout->addLayout(VLayout,1);

    HLayout2 = new QHBoxLayout;
    HLayout2->addWidget(ui->btn_main);
    HLayout2->addWidget(ui->btn_setting);
    HLayout2->addWidget(ui->btn_record);
    HLayout2->addWidget(ui->btn_password);
    HLayout2->addWidget(ui->btn_serial);



    VLayout2 = new QVBoxLayout;

    VLayout2->addLayout(HLayout);
    VLayout2->addLayout(HLayout2);

    this->setLayout(VLayout2);
    return;

}

void MainWidget::DrawBarChart(int num1, int num2, int num3, int num4)
{
    //第一步：创建一个QBarSet对象；QBarSet类代表条形图中的一组条形
    set0 = new QBarSet("传感器数据");
    *set0<<num1<<num2<<num3<<num4;

    //第二步：创建一个QBarSeries对象，QBarSeries类将一系列数据显示为按类别分组的竖线
    series = new QBarSeries;
    series->append(set0);
    series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);  //设置标签显示的位置
    series->setLabelsVisible(true);  //设置数据标签可见

    //第三步：创建一个QChart用来装QBarSeries对象；QChart是一个QGraphicsWidget，可以在QGraphicsScene中显示
    chart = new QChart;
    chart->addSeries(series);
    chart->setAnimationOptions(QChart::SeriesAnimations);

    //第四步：给柱状图添加一个坐标
    QStringList categories;
    categories << "光照" << "温度" << "湿度" << "紫外";  //保存横坐标字符串的列表
    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    chart->createDefaultAxes();  //创建一个默认的坐标轴
    chart->setAxisX(axis,series);//设置X坐标轴

    //如果已经存在柱状图 从水平布局中 移除原来的柱状图
    if(chartView !=NULL)
    {
        HLayout->removeWidget(chartView);
        delete chartView;
    }

    //第五步：创建一个QChartView来显示表格
    chartView = new QChartView(chart,this);
    chartView->resize(380, 350);
    HLayout->addWidget(chartView,4);
}

void MainWidget::on_btn_setting_clicked()
{
    this->hide();
    emit goSettingWidget();
}

void MainWidget::on_btn_record_clicked()
{
    this->hide();
    emit goRecordWidget();
}

void MainWidget::on_btn_password_clicked()
{
    this->hide();
    emit goPasswordWidget();
}

void MainWidget::on_btn_serial_clicked()
{
    this->hide();
    emit goSerialWindow();
}

void MainWidget::on_btn_submit_clicked()
{
    int num1 =  ui->lineEdit->text().toInt();
    int num2 =  ui->lineEdit_2->text().toInt();
    int num3 =  ui->lineEdit_3->text().toInt();
    int num4 =  ui->lineEdit_4->text().toInt();
    DrawBarChart(num1, num2, num3, num4);
}
